package com.wawu.server.controller;

import com.wawu.common.annotation.IOC.Autowired;
import com.wawu.common.annotation.IOC.component.RestController;
import com.wawu.common.annotation.controller.mapping.DeleteMapping;
import com.wawu.common.annotation.controller.mapping.GetMapping;
import com.wawu.common.annotation.controller.mapping.PostMapping;
import com.wawu.common.annotation.controller.mapping.RequestMapping;
import com.wawu.common.annotation.controller.parameter.RequestBody;
import com.wawu.common.annotation.controller.parameter.RequestParam;
import com.wawu.common.result.Result;
import com.wawu.pojo.dto.AddOrderDTO;
import com.wawu.pojo.dto.AskGoodsMsgDTO;
import com.wawu.pojo.dto.SettleAccountsDTO;
import com.wawu.pojo.vo.GetGoodsCommentVO.GoodCommentVO;
import com.wawu.pojo.vo.GoodVO;
import com.wawu.pojo.vo.OrderVO.OrderVO;
import com.wawu.pojo.vo.getGoodsInfo.getGoodsInfoVO;
import com.wawu.pojo.vo.getGoodsMsg.getGoodsMsgVO;
import com.wawu.pojo.vo.searchGoodVO.goodVO;
import com.wawu.server.service.MallService;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/mall")
public class MallController {
    @Autowired
    MallService mallService;


    /**
     * 通过商品类型获取商品列表
     * @param typeId
     * @return
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @GetMapping("/getGoodsByType")
    public Result<List<GoodVO>> getGoodsByType(@RequestParam Long typeId) throws SQLException, InvocationTargetException, IllegalAccessException {
//    public Result<List<GoodVO>> getGoodsByType(@RequestParam Long typeId) throws SQLException, InvocationTargetException, IllegalAccessException {
        return Result.success(mallService.getGoodVOsByTypeId(typeId));
    }

    //TODO 待完成：订单状态为完成时需要设置是否有评论
    /**
     * 通过商品状态获取订单（隐含了用户信息）
     *
     * @param state
     * @param token
     * @return
     */
    @GetMapping("/getOrderByState")
    public Result<List<OrderVO>> getOrderByState(@RequestParam Integer state, @RequestParam String token) throws SQLException {
        return Result.success(mallService.getOrderByState(state,token));
    }

    /**
     * 购物
     */
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody AddOrderDTO addOrderDTO) throws SQLException, InvocationTargetException, IllegalAccessException {
        mallService.addOrder(addOrderDTO);
        return Result.success();
    }


    /**
     * 购物车结算
     * @param settleAccountsDTO
     * @return
     * @throws SQLException
     */
    @PostMapping("/settleAccounts")
    public Result settleAccounts(@RequestBody SettleAccountsDTO settleAccountsDTO) throws SQLException {
        mallService.settleAccounts(settleAccountsDTO.getCartList());
        return Result.success();
    }

    /**
     * 获取商品详细信息
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping("/getGoodsInfo")
    public Result<getGoodsInfoVO> getGoodsById(@RequestParam("id") Long id) throws SQLException, InvocationTargetException, IllegalAccessException {

        return Result.success(mallService.getGoodsInfo(id));
    }

    /**
     * 搜索商品
     * @param keyword
     * @return
     * @throws SQLException
     */
    @GetMapping("/searchGoods")
    public Result<List<goodVO>> searchGoodsByKeyword(@RequestParam("keyword") String keyword) throws SQLException, InvocationTargetException, IllegalAccessException {

        return Result.success(mallService.searchGoodVOByKeyword(keyword));
    }

    /**
     * 获取商品问答信息
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping("/getGoodsMsg")
    public Result<List<getGoodsMsgVO>> getGoodsMsg(@RequestParam Long id ) throws SQLException, InvocationTargetException, IllegalAccessException {
        return Result.success(mallService.getGoodsMsgByGoodsId(id));
    }


    //DEGUG: 本处出错：这里返回的类型错误，不是数组
    /**
     * 获取商品的评论
     * @param goodsId
     * @return
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @GetMapping("/getGoodsComment")
//    public Result<List<GoodCommentVO>> getGoodsComment(@RequestParam Long goodsId) throws SQLException, InvocationTargetException, IllegalAccessException {
    public Result<GoodCommentVO> getGoodsComment(@RequestParam Long goodsId) throws SQLException, InvocationTargetException, IllegalAccessException {
        return Result.success(mallService.getGoodsCommentByGoodsId(goodsId));
    }


    /**
     * 商品问答
     * @param askGoodsMsgDTO
     * @return
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @PostMapping("/askGoodsMsg")
    public Result askGoodsMsg (@RequestBody AskGoodsMsgDTO askGoodsMsgDTO) throws SQLException, InvocationTargetException, IllegalAccessException {
        mallService.askGoodsMsg(askGoodsMsgDTO);
        return Result.success();
    }

    /**
     * 根据订单id删除订单
     * @param orderId
     * @return
     */
    @DeleteMapping("/deleteOrder")
    public Result deleteOrderById(@RequestParam("id") Long orderId) throws SQLException {
        mallService.deleteOrderById(orderId);
        return Result.success();
    }




    /*@GetMapping("/getGoodsByType")
    public Object mookgetGoodsByType() {


        // 创建商品数据
        String goodsJson = "{\n" +
                "  \"code\": 0,\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"id\": \"13\",\n" +
                "      \"img\": \"https://th.bing.com/th/id/OIP.F0yTEUOfgjk9R7eeTrzXBQAAAA?w=201&h=201&c=7&r=0&o=5&dpr=1.6&pid=1.7\",\n" +
                "      \"name\": \"衣服3\",\n" +
                "      \"price\": 0,\n" +
                "      \"typeId\": \"2\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"12\",\n" +
                "      \"img\": \"https://th.bing.com/th/id/OIP.F0yTEUOfgjk9R7eeTrzXBQAAAA?w=201&h=201&c=7&r=0&o=5&dpr=1.6&pid=1.7\",\n" +
                "      \"name\": \"衣服2\",\n" +
                "      \"price\": 0,\n" +
                "      \"typeId\": \"2\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"11\",\n" +
                "      \"img\": \"https://th.bing.com/th/id/OIP.F0yTEUOfgjk9R7eeTrzXBQAAAA?w=201&h=201&c=7&r=0&o=5&dpr=1.6&pid=1.7\",\n" +
                "      \"name\": \"衣服1\",\n" +
                "      \"price\": 0,\n" +
                "      \"typeId\": \"2\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"10\",\n" +
                "      \"img\": \"https://th.bing.com/th/id/OIP.y7NkyP6FJmAOP_XsY4i9rAHaF7?w=225&h=180&c=7&r=0&o=5&dpr=1.6&pid=1.7\",\n" +
                "      \"name\": \"Eletronics7\",\n" +
                "      \"price\": 10,\n" +
                "      \"typeId\": \"1\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"9\",\n" +
                "      \"img\": \"https://th.bing.com/th/id/OIP.y7NkyP6FJmAOP_XsY4i9rAHaF7?w=225&h=180&c=7&r=0&o=5&dpr=1.6&pid=1.7\",\n" +
                "      \"name\": \"Eletronics6\",\n" +
                "      \"price\": 10,\n" +
                "      \"typeId\": \"1\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"8\",\n" +
                "      \"img\": \"https://th.bing.com/th/id/OIP.y7NkyP6FJmAOP_XsY4i9rAHaF7?w=225&h=180&c=7&r=0&o=5&dpr=1.6&pid=1.7\",\n" +
                "      \"name\": \"Eletronics5\",\n" +
                "      \"price\": 10,\n" +
                "      \"typeId\": \"1\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"7\",\n" +
                "      \"img\": \"https://th.bing.com/th/id/OIP.y7NkyP6FJmAOP_XsY4i9rAHaF7?w=225&h=180&c=7&r=0&o=5&dpr=1.6&pid=1.7\",\n" +
                "      \"name\": \"Eletronics4\",\n" +
                "      \"price\": 10,\n" +
                "      \"typeId\": \"1\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"6\",\n" +
                "      \"img\": \"https://th.bing.com/th/id/OIP.y7NkyP6FJmAOP_XsY4i9rAHaF7?w=225&h=180&c=7&r=0&o=5&dpr=1.6&pid=1.7\",\n" +
                "      \"name\": \"Eletronics3\",\n" +
                "      \"price\": 10,\n" +
                "      \"typeId\": \"1\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"5\",\n" +
                "      \"img\": \"https://th.bing.com/th/id/OIP.y7NkyP6FJmAOP_XsY4i9rAHaF7?w=225&h=180&c=7&r=0&o=5&dpr=1.6&pid=1.7\",\n" +
                "      \"name\": \"Eletronics2\",\n" +
                "      \"price\": 10,\n" +
                "      \"typeId\": \"1\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"4\",\n" +
                "      \"img\": \"https://th.bing.com/th/id/OIP.y7NkyP6FJmAOP_XsY4i9rAHaF7?w=225&h=180&c=7&r=0&o=5&dpr=1.6&pid=1.7\",\n" +
                "      \"name\": \"Eletronics1\",\n" +
                "      \"price\": 10,\n" +
                "      \"typeId\": \"1\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"1\",\n" +
                "      \"img\": \"http://example.com/smartphone.jpg\",\n" +
                "      \"name\": \"Smartphone\",\n" +
                "      \"price\": 699.99,\n" +
                "      \"typeId\": \"1\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"2\",\n" +
                "      \"img\": \"http://example.com/tshirt.jpg\",\n" +
                "      \"name\": \"T-Shirt\",\n" +
                "      \"price\": 19.99,\n" +
                "      \"typeId\": \"2\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"3\",\n" +
                "      \"img\": \"http://example.com/book.jpg\",\n" +
                "      \"name\": \"Fiction Book\",\n" +
                "      \"price\": 9.99,\n" +
                "      \"typeId\": \"3\"\n" +
                "    }\n" +
                "  ]\n" +
                "}";

        // 使用 fastjson 解析 JSON 字符串
        JSONObject jsonObject = JSON.parseObject(goodsJson);
        return jsonObject;
    }*/

}
